package cn.com.zzl.util;

import java.util.Objects;

import cn.com.zzl.po.Student;

/**
 * Description：java.util.Objects工具类（jdk1.7）
 * Created by 朱正磊 on 2017-08-21 9:46.
 */
public class ObjectsTest {

	/**
	 * @Desc 测试java.util.Objects里的方法
	 * @param args 参数
	 */
	public static void main(String[] args) {
		Student obj_01 = new Student("zhangsan", 30);
		Student obj_02 = new Student("zhangsan", 20);

		//01. Objects.compare 比较大小
		/*System.out.println("01. Objects.compare----->" + Objects.compare(obj_01, obj_02, new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o1.getAge().compareTo(o2.getAge());
			}
		}));*/

		//01. Objects.compare 比较大小
		//System.out.println("01. Objects.compare----->" +
				//Objects.compare(obj_01, obj_02, (o1, o2) -> o1.getAge().compareTo(o2.getAge())));// 1

		//02. Objects.deepEquals 深度比较是否相等（不常用）
		System.out.println("02. Objects.deepEquals----->" + Objects.deepEquals(obj_01, obj_02));// true

		//03. Objects.equals 比较是否相等
		System.out.println("03. Objects.equals----->" + Objects.equals(obj_01, obj_02));// true

		//04. Objects.hash 获取对象数组hash值
		Object[] obj_04 = new Object[]{obj_01, obj_02};
		System.out.println("04. Objects.hash---->" + Objects.hash(obj_04));// 1911 固定

		//05. Objects.hashCode 获取对象的hashCode
		System.out.println("05. Objects.hashCode---->" + Objects.hashCode(obj_01));// 30 固定

		//06. Objects.isNull 为null是true
		//Object obj_06 = null;
		//System.out.println("06. Objects.isNull----->" + Objects.isNull(obj_06));// true

		//07. Objects.nonNull 为null是false
		//Object obj_07 = null;
		//System.out.println("07. Objects.nonNull---->" + Objects.nonNull(obj_07));// false

		//08. Objects.requireNonNull，如果为null，输出Exception in thread "main" java.lang.NullPointerException
		Object obj_08 = 1;
		System.out.println("08. Objects.requireNonNull----->" + Objects.requireNonNull(obj_08));// 1

		//09. Objects.requireNonNull，如果为null，输出Exception in thread "main" java.lang.NullPointerException: The object cann't be null.
		Object obj_09 = 1;
		System.out.println("09. Objects.requireNonNull----->" +
				Objects.requireNonNull(obj_09, "The object cann't be null."));// 1

		//10. Objects.requireNonNull
		/*Object obj_10 = 1;
		System.out.println("10. Objects.requireNonNull----->" + Objects.requireNonNull(obj_10, new Supplier<String>() {
			@Override
			public String get() {
				return "The object cann't be null.";
			}
		}));*/

		//10. Objects.requireNonNull，如果为null，输出Exception in thread "main" java.lang.NullPointerException: The object cann't be null...
		//Object obj_10 = 1;
		//System.out.println("10. Objects.requireNonNull----->" +
				//Objects.requireNonNull(obj_10, () -> "The object cann't be null..."));// 1

		//11. Objects.toString
		Object obj_11 = "123";
		System.out.println("11. Objects.toString----->" + Objects.toString(obj_11));// 123

		//12. Objects.toString 如果第一个参数不为null就输出第一个参数，否则输出第二个参数
		Object obj_12 = "222";
		System.out.println("12. Objects.toString----->" +
				Objects.toString(obj_12, "234") + "<----->" + Objects.toString(null, "234"));// 222 234

	}

}
